package com.zk.yygh.user.controller.api;

import com.zk.yygh.hosp.result.Result;
import com.zk.yygh.hosp.utils.AuthContextHolder;
import com.zk.yygh.model.user.Patient;
import com.zk.yygh.user.service.PatientService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author: 张凯
 * @date: 2022/6/17 - 18:35
 */
@Api("就诊管理接口")
@RestController
@RequestMapping("/api/user/patient")
public class PatientController {

    @Autowired
    private PatientService patientService;

    @ApiOperation(value = "添加就诊人")
    @PostMapping("auth/save")
    public Result savePatientList(@RequestBody Patient patient,HttpServletRequest request){
        //获取认证用户的id
        Long userId = AuthContextHolder.getUserId(request);
        //给就诊人设置认证用户的id
        patient.setUserId(userId);
        //添加就诊人
        patientService.save(patient);
        return Result.ok();
    }

    @ApiOperation(value = "删除就诊人")
    @DeleteMapping("auth/remove/{id}")
    public Result removePatient(@PathVariable("id") Long id){
        patientService.removeById(id);
        return Result.ok();
    }

    @ApiOperation(value = "查询就诊人")
    @GetMapping("auth/getPatient/{id}")
    public Result getPatient(@PathVariable("id") Long id){
        Patient patient = patientService.getById(id);
        return Result.ok(patient);
    }

    @ApiOperation(value = "修改就诊人")
    @GetMapping("auth/update")
    public Result updatePatient(@RequestBody Patient patient){
        patientService.updateById(patient);
        return Result.ok();
    }

    @ApiOperation(value = "获取就诊人列表")
    @GetMapping("auth/patientList")
    public Result getPatientList(HttpServletRequest request){
        List<Patient> patientList = patientService.getPatientList(AuthContextHolder.getUserId(request));
        return Result.ok(patientList);
    }

    @ApiOperation("根据就诊id获取就诊人，供远程调用")
    @GetMapping("inner/get/{id}")
    Patient getPatientById(@PathVariable("id") Long id){
        Patient patient = patientService.getById(id);
        return patient;
    }
}
